/**
* Created by yyr on 2018/1/15
*已设立基金选项卡基金合伙人
*/
<template>
    <div>
        <div class="ca-box-border-top-none ca-p-22">
            <div class="ca-title">
                <h3>基金合伙人</h3>
                <div class="ca-pull-right ca-p-b-20">
                    <el-button  type="primary" class="ca-m-l-10" @click="addPartner" :disabled="!isAuthPartnerEdit">
                        添加
                    </el-button>
                </div>
            </div>
            <!--列表-->
            <div class="ca-m-t-15" v-for="(row,index) in fundPartner"  :key="index" :class="[{'ca-m-t-30':index>0}]">
                <div class="ca-title" v-if="!row.editStatus">
                    <h3>基金合伙人信息</h3>
                    <div class="ca-pull-left ca-m-l-20" style="line-height:36px;font-size:14px;color:#666">
                        {{$moment( row.fundPartnerEntity.recordingTime ).format("YYYY-MM-DD")}}
                    </div>
                    <div class="ca-pull-right">
                        <button type="button" @click="partnerOpen(index)" class="ca-btn-switch ca-btn-default ca-m-l-10" v-if="row.show"><i class="el-icon-arrow-down ca-m-r-5"></i><span>收起</span></button>
                        <button type="button" @click="partnerOpen(index)" class="ca-btn-switch ca-btn-default ca-m-l-10" v-else><i class="el-icon-arrow-up ca-m-r-5"></i><span>展开</span></button>
                        <el-button type="text" @click="editPartnerItem(row)" :disabled="!isAuthPartnerEdit" class="ca-edit-btn el-icon-edit ca-m-l-20">编辑</el-button>
                    </div>
                </div>
                <div class="ca-title" v-if="row.editStatus">
                    <h3>合伙人信息</h3>
                    <div class="ca-pull-left ca-m-l-20">
                        <el-date-picker type="date" v-model='row.fundPartnerEntity.recordingTime' placeholder="选择日期" style="width:150px" class="ca-m-r-15"></el-date-picker>
                    </div>
                    <div class="ca-pull-right">
                        <el-button type="danger" size="medium" @click="deletePartner(row)">删除</el-button>
                        <el-button type="primary" size="medium" @click="saveEdit(row)">保存</el-button>
                        <el-button size="medium" @click="cancelEdit(row)">取消</el-button>
                    </div>
                </div>
                <div class="ca-m-t-5" v-if="!row.editStatus">
                    <div style="line-height:20px;">
                        <div style="float: left;color:#999;font-size:14px">变更原因：</div><div style="margin-left:68px;color:#666666;font-size:14px;">{{row.fundPartnerEntity.reason}}</div>
                    </div>
                    <el-table
                        v-if="row.show"
                        :data="row.distHistoryList"
                        stripe
                        border
                        style="width:100%" class="table_p_01 ca-m-t-15 ca-table">
                        <el-table-column
                            show-overflow-tooltip
                            label="合伙人类型">
                            <template slot-scope="scope">
                                {{scope.row.type | csConstFilter('PARTNER_TYPE')}}
                            </template>
                        </el-table-column>
                        <el-table-column
                            show-overflow-tooltip
                            prop="customerName"
                            label="合伙人名称">
                            <template slot-scope="scope">
                                <el-button
                                   type="text"
                                   @click="goPartnerDetail(scope.row.customerId)"
                                   :disabled="scope.row.existence">{{scope.row.customerName}}
                                </el-button>
                            </template>
                        </el-table-column>
                        <el-table-column
                            show-overflow-tooltip
                            prop="stages"
                            label="出资分期">
                            <template slot-scope="scope">
                                {{scope.row.stages}}
                            </template>
                        </el-table-column>
                        <el-table-column
                            show-overflow-tooltip
                            prop="life"
                            label="缴纳年限">
                            <template slot-scope="scope">
                                {{scope.row.life}}
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop="subscribed"
                            label="认缴金额(万元)">
                            <template slot-scope="scope">
                                {{scope.row.subscribed | format1}}
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop="paid"
                            label="实缴金额(万元)">
                            <template slot-scope="scope">
                                {{scope.row.paid | format1}}
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop="proportion"
                            label="当前权益(%)">
                            <template slot-scope="scope">
                                {{scope.row.proportion}}
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="持有状态">
                            <template slot-scope="scope">
                                {{scope.row.holdingState | getNameById('fundHolding')}}
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <div class="ca-m-t-5" v-if="row.editStatus">
                    <el-row class="ca-m-t-15">
                        <el-col :span="2">变更原因</el-col>
                        <el-col class="ca-m-l-20" :span="16">
                            <el-input  v-model='row.fundPartnerEntity.reason' type="textarea" :maxlength="400" placeholder="请输入内容"></el-input>
                        </el-col>
                    </el-row>
                    <table class="ca-table ca-m-t-15">
                        <tr>
                            <th width="10%">合伙人类型</th>
                            <th width="10%">合伙人名称</th>
                            <th width="10%">出资分期</th>
                            <th width="10%">缴纳年限</th>
                            <th width="10%">认缴金额(万元)</th>
                            <th width="10%">实缴金额(万元)</th>
                            <th width="10%">持有状态</th>
                            <th width="10%" class="p-tb-0"><button type="button" class="ca-btn ca-btn-round" @click="editPartnerTableData(row)"><i class="fa fa-plus"></i></button></th>
                        </tr>
                        <tr v-for="(list,index1) in row.distHistoryList">
                            <td>
                                <el-select v-model="list.type" placeholder="请选择" style="width:100%">
                                    <el-option
                                        v-for="item in initView.type"
                                        :label="item.name"
                                        :key="item.id"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </td>
                            <td>
                                <el-select v-model="list.customerId"
                                           @change="changePartnerEdit(list,list.customerId)"
                                           filterable placeholder="请选择" style="width:100%;">
                                    <el-option
                                        v-for="item in initView.userOptions"
                                        :key="item.value"
                                        :label="item.guestName"
                                        :value="item.guestId">
                                    </el-option>
                                </el-select>
                            </td>
                            <td><el-input placeholder="请输入内容" type="number" v-model="list.stages" class="ca-vertical-align-middle"></el-input></td>
                            <td><el-input v-model="list.life" type="number" placeholder="请输入内容" class="ca-vertical-align-middle"></el-input></td>
                            <td><el-input v-model="list.subscribed" type="number" placeholder="请输入内容" class="ca-vertical-align-middle"></el-input></td>
                            <td><el-input v-model="list.paid" placeholder="请输入内容" type="number"  class="ca-vertical-align-middle"></el-input></td>
                            <td>
                                <el-select v-model="list.holdingState" placeholder="请选择" style="width:100%">
                                    <el-option
                                        v-for="item in initView.fundHolding"
                                        :label="item.name"
                                        :key="item.id"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </td>
                            <td class="p-tb-0">
                                <button type="button" class="ca-btn ca-btn-round" @click="deletePartnerSingle(list,index1)"><i class="fa fa-remove"></i></button>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
        <!--添加弹出框-->
        <el-dialog title="" :visible.sync="dialogFormVisible" width="80%"
                   :before-close="cancelDialog" :show-close="false">
            <div class="ca-m-t-0">
                <div class="ca-title">
                    <h3>合伙人认缴信息</h3>
                    <div class="ca-pull-left ca-m-l-20">
                        <el-date-picker type="date"  v-model='partnerListAdd.fundPartnerEntity.recordingTime' placeholder="选择日期" style="width: 150px;"></el-date-picker>
                    </div>
                    <div class="clear"></div>
                </div>
                <div>
                    <el-row class="ca-m-t-15">
                        <el-col :span="2">变更原因</el-col>
                        <el-col :span="22">
                            <el-input  v-model='partnerListAdd.fundPartnerEntity.reason' type="textarea" :maxlength="400" placeholder="请输入内容"></el-input>
                        </el-col>
                    </el-row>
                    <table class="ca-table ca-m-t-15">
                        <tr>
                            <th width="10%">合伙人类型</th>
                            <th width="10%">合伙人名称</th>
                            <th width="10%">出资分期</th>
                            <th width="10%">缴纳年限</th>
                            <th width="10%">认缴金额(万元)</th>
                            <th width="10%">实缴金额(万元)</th>
                            <th width="10%">持有状态</th>
                            <th width="10%" class="p-tb-0"><button type="button" class="ca-btn ca-btn-round" @click="addPartnerTableData()"><i class="fa fa-plus"></i></button></th>
                        </tr>
                        <tr v-for="(list,index) in partnerListAdd.distHistoryList" :key="index">
                            <td>
                                <el-select v-model="list.type" placeholder="请选择" style="width:100%">
                                    <el-option
                                        v-for="item in initView.type"
                                        :label="item.name"
                                        :key="item.id"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </td>
                            <td>
                                <el-select v-model="list.customerId"
                                           @change="changePartner(index)"
                                           filterable placeholder="请选择" style="width:100%;">
                                    <el-option
                                        v-for="item in initView.userOptions"
                                        :key="item.value"
                                        :label="item.guestName"
                                        :value="item.guestId">
                                    </el-option>
                                </el-select>
                            </td>
                            <td><el-input placeholder="请输入内容" type="number" v-model="list.stages" class="ca-vertical-align-middle"></el-input></td>
                            <td><el-input v-model="list.life" type="number" placeholder="请输入内容" class="ca-vertical-align-middle"></el-input></td>
                            <td><el-input v-model="list.subscribed" type="number" placeholder="请输入内容" class="ca-vertical-align-middle"></el-input></td>
                            <td><el-input v-model="list.paid" placeholder="请输入内容" type="number"  class="ca-vertical-align-middle"></el-input></td>
                            <td>
                                <el-select v-model="list.holdingState" placeholder="请选择" style="width:100%">
                                    <el-option
                                        v-for="item in initView.fundHolding"
                                        :label="item.name"
                                        :key="item.id"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </td>
                            <td class="p-tb-0">
                                <button type="button" class="ca-btn ca-btn-round" @click="deletePartnerSingle(list,-1)"><i class="fa fa-remove"></i></button>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelDialog()">取 消</el-button>
                <el-button type="primary" @click="addPartnerData()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script type="text/ecmascript-6">
    import {api} from '../api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    import {isNoAuth} from '__service/utils';
    export default {
        data() {
            return {
                isAuthPartnerEdit: isNoAuth('已设立基金-基金合伙人-编辑'),
                itemId: this.$route.params.fundId,
                dialogFormVisible: false,
                fundPartner: [],
                initView: {
                    type: this.$store.state.csConst.PARTNER_TYPE, //合伙人类型
                    fundHolding: this.$store.state.user.dictionaryMap.fundHolding,//持有状态
                    userOptions: this.$store.state.user.guest,//客户
                },
                partnerList: { //+号点击添加表格一行
                    attribute: '',  //资金属性
                    customerId: '', //合伙人Id
                    customerName: '',//合伙人name
                    holdingState: '',//持有状态 fundHolding
                    id: '',  //
                    life: '',//缴纳年限
                    paid: 0,//实缴金额
                    partnerId: '', //已设立基金合伙人主id
                    partnerType: 1, //合伙人类型1 客户，2 母基金
                    stages: '', //出资分期
                    subscribed: '', //认缴金额
                    type: 0, //合伙人类型1 Lp 2 Gp
                },
                partnerListAdd: {   //保存时数据
                    distHistoryList: [{
                        attribute: '',  //资金属性
                        customerId: '', //合伙人Id
                        customerName: '',//合伙人name
                        holdingState: '',//持有状态 fundHolding
                        id: '',  //
                        life: '',//缴纳年限
                        paid: 0,//实缴金额
                        partnerId: '', //已设立基金合伙人主id
                        partnerType: 1, //合伙人类型1 客户，2 母基金
                        stages: '', //出资分期
                        subscribed: '', //认缴金额
                        type: 1, //合伙人类型1 Lp 2 Gp
                    }],
                    fundPartnerEntity: {
                        fundId: this.$route.params.fundId,
                        id: '',
                        reason: '',
                        recordingTime: '',
                    }
                },
                partnerName: '',
                customerId: '',
            }
        },
        props: {
        },
        created() {
            this.getPartnerList();
        },
        mounted() {
        },
        methods: {
            //获取合伙人列表
            getPartnerList() {
                apiHttp(api.PARTNER_LIST,{fundId: this.itemId}).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        for ( let i of ret.data) {
                            i.show = false;
                            i.hide = true;
                            i.editStatus = false;
                        }
                        this.fundPartner = ret.data;
                     }
                })
            },
            //添加合伙人信息，弹出框
            addPartner() {
                this.dialogFormVisible = true;
                this.partnerListAdd = {
                    distHistoryList: [{
                        attribute: '',  //资金属性
                        customerId: '', //合伙人Id
                        customerName: '',//合伙人name
                        holdingState: '',//持有状态 fundHolding
                        id: '',  //
                        life: '',//缴纳年限
                        paid: 0,//实缴金额
                        partnerId: '', //已设立基金合伙人主id
                        partnerType: 1, //合伙人类型1 客户，2 母基金
                        stages: '', //出资分期
                        subscribed: '', //认缴金额
                        type: 1, //合伙人类型1 Lp 2 Gp
                    }],
                    fundPartnerEntity: {
//                        fundId: this.$route.params.fundId,
                        fundId: this.$route.params.fundId,
                        id: '',
                        reason: '',
                        recordingTime: '',
                    }
                }
            },
            //弹出框的取消按钮
            cancelDialog() {
                this.dialogFormVisible = false;
            },
            //弹出框的确定按钮 保存合伙人信息接口
            addPartnerData() {
                this.partnerListAdd.fundPartnerEntity.fundId = this.itemId;
                if (!this.partnerListAdd.fundPartnerEntity.recordingTime) {
                    this.$message.error('请选择日期');
                    return false;
                }
                for (let i of this.partnerListAdd.distHistoryList) {
                    if (i.customerName === '') {
                        this.$message.error('请选择合伙人');
                        return false;
                    }
                }
                apiHttp(api.ADD_PARTNER,this.partnerListAdd).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.$message({
                            message: '添加成功',
                            type: 'success',
                            center: true
                        });
                        this.$emit('addRedDot', 1)
                        this.dialogFormVisible = false;
                        this.getPartnerList();
                    }
                })
            },
            //添加一行空的
            addPartnerTableData() {
                this.partnerListAdd.distHistoryList.push(Object.assign({},this.partnerList))
            },
            editPartnerTableData(row) {
                row.distHistoryList.push(Object.assign({},this.partnerList))
            },
            //删除表格中一行
            deletePartnerSingle(list,index) {
                if (index === -1) { //添加时候删除一行，直接截取
                    this.partnerListAdd.distHistoryList.splice(index, 1);
                } else {//修改时删除
                    this.$confirm('是否删除此条合伙人信息?', '提示', {
                        cancelButtonText: '取消',
                        confirmButtonText: '确定',
                        type: 'warning',
                        showClose: false
                    }).then(async () => {
                        apiHttp(api.DELETE_SINGLE_PARTNER,{id: list.id}).then( ret=>{
                            if (ret.resCode === STATUS_HTTP_SUCCESS) {
                                this.$message({
                                    type: 'success',
                                    message: '删除成功!',
                                    center: true
                                });
                                this.getPartnerList();
                            }
                        })
                    });
                }
            },
            //添加时候改变合伙人
            changePartner(index) {
                let arrId = this.partnerListAdd.distHistoryList[index].customerId;
                this.initView.userOptions.map( item=>{
                    if (arrId === item.guestId) {
                        this.partnerListAdd.distHistoryList[index].customerName = item.guestName;
                    }
                })
            },
            //编辑改变合伙人
            changePartnerEdit(list,listId) {
                this.initView.userOptions.map( item =>{
                    if (listId === item.guestId) {
                        list.customerName = item.guestName;
                    }
                })
            },
            //合伙人展开收起
            partnerOpen(index) {
                if (this.fundPartner[index].show === false) {
                    this.fundPartner[index].show = true;
                    this.fundPartner[index].hide = false
                } else {
                    this.fundPartner[index].show = false;
                    this.fundPartner[index].hide = true
                }
            },
            //编辑单个合伙人信息
            editPartnerItem(row) {
                row.editStatus = true;
            },
            //取消编辑
            cancelEdit(row) {
                row.editStatus = false;
                this.getPartnerList();
            },
            //删除一个合伙人信息
            deletePartner(row) {
                this.$confirm('是否删除此条合伙人信息?', '提示', {
                    cancelButtonText: '取消',
                    confirmButtonText: '确定',
                    type: 'warning',
                    showClose: false
                }).then(async () => {
                    apiHttp(api.DELETE_PARTNER,{id: row.fundPartnerEntity.id}).then( ret=>{
                        if (ret.resCode === STATUS_HTTP_SUCCESS) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!',
                                center: true
                            });
                            row.editStatus = false;
                            this.getPartnerList();
                        }
                    })
                });
            },
            //保存编辑
            saveEdit(row) {
                for (let i of row.distHistoryList) {
                    if (i.customerName === '') {
                        this.$message.error('请选择合伙人');
                        return false;
                    }
                }
                let data = Object.assign({},row);
                data.distHistoryList.map(item=>{
                    delete item.existence
                    delete item.proportion
                })
                let query = {
                    distHistoryList: data.distHistoryList,
                    fundPartnerEntity: row.fundPartnerEntity
                }
                apiHttp(api.CHANGE_PARTNER,query).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.$message({
                            type: 'success',
                            message: '修改成功!',
                            center: true
                        });
                        this.$emit('addRedDot', 1)
                        this.cancelEdit(row);
                    }
                })
            },
            //跳转合伙人
            goPartnerDetail(id) {
                apiHttp(api.GET_GUEST,{guestId: id}).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.$router.push({name: 'client-management-info', params: {eClient_guestId: id, openWindow: true }});
                    }
                })
            }
        },
        components: {
        },
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    .ca-title{
        height:42px;
    }
    table td{
        padding: 5px 5px;
        box-sizing: border-box;
    }
</style>
